library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gt)
library(htmltools)


# Esto es para cada una de las categorias:

iris %>% 
  filter(Species == "setosa") %>% 
  ggplot() +
  geom_point(aes(x = Sepal.Width,y=Sepal.Length))

iris %>% 
  filter(Species == "virginica") %>% 
  ggplot() +
  geom_point(aes(x = Sepal.Width,y=Sepal.Length))

iris %>% 
  filter(Species == "versicolor") %>% 
  ggplot() +
  geom_point(aes(x = Sepal.Width,y=Sepal.Length))

# En su lugar podemos un split y utilizando un map:


iris_plot_lista <- iris %>% 
  split(.$Species) %>% 
  map(~.x %>% 
        ggplot() +
        geom_point(aes(x = Sepal.Width,y=Sepal.Length)))

names(iris_plot_lista)
## [1] "setosa"     "versicolor" "virginica"
iris_plot_lista[[1]]

iris_plot_lista[[2]]

iris_plot_lista[[3]]

# Anidar la información en una columna y siempre trabajar dentro de un data.frame:

nest_iris <- iris %>% 
  group_by(Species) %>% 
  nest_by()


nest_iris_2 <- nest_iris %>% 
  mutate(plot_point = list(ggplot(data ) +
                             geom_point(aes(x = Sepal.Width,y=Sepal.Length)))
  )


plotly::ggplotly(iris_plot_lista[[1]])
nest_iris_2 <- nest_iris_2 %>% 
  mutate(plotly_list = list(plotly::ggplotly(plot_point)))

nest_iris_2 <- nest_iris_2 %>% 
  mutate(plotly_list = list(plotly_list %>%
           as.tags() %>%
           as.character() %>%
           htmltools::HTML()))


nest_iris_3 <- nest_iris_2 %>% 
  select(-data,-plot_point) 

  
  DT::datatable(nest_iris_3, escape = FALSE,options = list(
  fnDrawCallback = htmlwidgets::JS(
    '
function(){
  HTMLWidgets.staticRender();
}
'
  )))

Termometro de la tendencia politica:


                                
$0-1   1-2   2-3   3-4   4-5   5-6   6-7   7-8   8-9   9-10 

enlaces <- c(
"https://thumbs.dreamstime.com/z/flor-del-iris-setosa-del-iris-67468042.jpg",

"https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Iris_virginica.jpg/250px-Iris_virginica.jpg",

"https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.consejosparamihuerto.com%2Ffamilias%2Firis-versicolore%2F&psig=AOvVaw0k6NQUBUn92QtLkqLrRd27&ust=1636636896960000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCLjx1NfxjfQCFQAAAAAdAAAAABAD")


nest_iris_4 <- nest_iris_3 %>% 
  mutate(
    src = list(enlaces),
    Species = list(str_c('<div>',Species,'</b><br><img src="',src,'">',
                         "</div>"))) %>% 
  select(-src)


  DT::datatable(nest_iris_4, escape = FALSE,options = list(
  fnDrawCallback = htmlwidgets::JS(
    '
function(){
  HTMLWidgets.staticRender();
}
'
  )))